package org.jpass.dc.view.file;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.jpass.dc.business.project.logic.FileService;
import org.jpass.dc.business.project.logic.ProjectService;
import org.jpass.dc.common.bean.TreeNode;
import org.jpass.dc.common.util.StringUtil;
import org.jpass.dc.view.common.BasicAction;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
 * 
 * @author WeiLong Peng
 *
 */
public class GetFolderListAction extends BasicAction {

	private static final long serialVersionUID = -5374235522755096645L;
	
    private Log log = LogFactory.getLog(getClass());
	
    private ProjectService projectService = new ProjectService();
    
	private FileService fileService = new FileService();
	
	private Integer depth;
	
	private String parentPath;
	
	@Override
	public String execute() throws Exception {
		
		List<TreeNode> folderList = new ArrayList<TreeNode>();
		if (depth == 0) {
			folderList = projectService.getList(this.getCurrentUser());
		} else {
			if (!StringUtil.isBlank(parentPath)) {
				String path = this.getUserRealWrokRootPath();
				folderList = fileService.getFolderList(path + "/" + parentPath);
			}
		}
		
		Gson gson= new GsonBuilder().create();
		String json = "";
		if (folderList != null) {
			json = gson.toJson(folderList);
		}
		
		try {
			ServletActionContext.getResponse().setCharacterEncoding("utf-8");
			ServletActionContext.getResponse().getWriter().print(json);
		} catch (IOException e) {
			e.printStackTrace();
			log.error("请求目录列表时获取PrintWriter时抛出异常：", e);
		}
		return null;
	}
	
	public Integer getDepth() {
		return depth;
	}

	public void setDepth(Integer depth) {
		this.depth = depth;
	}

	public String getParentPath() {
		return parentPath;
	}

	public void setParentPath(String parentPath) {
		this.parentPath = parentPath;
	}
}
